package com.hs.chd.dataprocess;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import com.hs.core.db.DBMgr;
import com.hs.core.utils.TimeUtil;
 

public class FileContentProcess {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
        DBMgr dbo = DBMgr.getDbUtilInst();
        List datalist = dbo.queryList("select id ,bookname from SCHD_OLDPAPER", null);
        int countall = 0;
        for(int i =0;i<datalist.size();i++){
        	Map data = (Map)datalist.get(i);
        	Long id = ((java.math.BigDecimal)data.get("id")).longValue();
        	String bookname = (String)data.get("BOOKNAME");
        	
        	File dir = new File("E:\\project\\脑病古今文献数据库\\01客户资料\\脑病文献库最终版本\\books\\"+bookname);
//        	if(!dir.exists()){
//        		System.out.println(id+":dir:" + dir.getAbsolutePath() );
//        	}
        	List filelist = (List)FileUtils.listFiles(dir, new String[]{"txt"}, false);
        	for(int j=0;j<filelist.size();j++){
        		//System.out.println(bookname+":" +filelist.size() );
        		File textfile = (File)filelist.get(j);
        		String filename =textfile.getName();
        		int index = filename.indexOf(".");
        		String ordernum = filename.substring(index-2,index);
        		//System.out.println(filename+":"+ordernum);
        		InputStream ins = new FileInputStream(textfile);    
        		String chararr = IOUtils.toString(ins,"GBK");
                dbo.update("INSERT INTO SCHD_FILEINFO (ID, PAPERID, PRODATE, PROVERSION, PAPERTITLE, PAPERTEXTCONTENT, CONTENTTYPE, ORDERNUM) VALUES ( SEQ_SCHD_FILEINFO_ID.nextval, ?,?,1,?,?,2,?)", new Object[]{id,TimeUtil.getCurrentDate(),filename,chararr,ordernum}) ; 
        		System.out.println("status count:" + countall );

        		countall++;
        	}
        }
        System.out.println("countall:" +countall);
	}

}
